﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemySpawnPoint : MonoBehaviour {
    public List<GameObject> waitSpawnEneny = new List<GameObject>();
    public float spawnSpeed = 0.5f;
    public bool canSpawnEnemys = false;

    float Timer = 0;
    int enemysIndex = 0;
    private void Start()
    {
        LevelManager.Instance.changeLevel += InitWaitEnemys;
    }
    private void OnDestroy()
    {
        LevelManager.Instance.changeLevel -= InitWaitEnemys;
    }
    void InitWaitEnemys(LevelInfo levelInfo)
    {
        waitSpawnEneny = levelInfo.enemys;
        enemysIndex = 0;
        //Debug.Log(waitSpawnEneny[enemysIndex].name);
    }


    private void Update()
    {
        if (canSpawnEnemys)
        {
            Timer += Time.deltaTime;
            if (Timer>spawnSpeed)
            {
                Timer = 0;
                SpawnEnemy();
            }
        }
    }
    public void SpawnEnemy()
    {
        if (enemysIndex<waitSpawnEneny.Count)
        {
            GameObject obj= Instantiate(waitSpawnEneny[enemysIndex], transform.position, transform.rotation);
            enemysIndex++;
            GameManager.Instance.AddEnemy(obj);
        }
        else
        {
            canSpawnEnemys = false;
        }

    }

    public void SetCanSpawnEnemy(){
        canSpawnEnemys=true;

    }

}
